home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6910 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  63 lines

  1. Newsgroups: comp.lang.c++
  2. Path: phcoms4.seri.philips.nl!philce!hartj
  3. From: hartj@ce.philips.nl (Joost 't Hart)
  4. Subject: Re: Syntax clarification
  5. Message-ID: <Dn33Ft.H9J@ce.philips.nl>
  6. Sender: usenet@ce.philips.nl (USENET News System)
  7. Nntp-Posting-Host: adcsun10
  8. Organization: Philips Consumer Electronics, Eindhoven, The Netherlands
  9. References: <31236966.1881@sisna.com>
  10. Date: Tue, 20 Feb 1996 17:04:36 GMT
  11.  
  12. Matt Smolic <msmolic@sisna.com> writes:
  13.  
  14. >I am just making the switch from C to C++. Something I have come across 
  15. >and cannot find an answer to is the use of the "&" symbol. In C this 
  16. >means the address of, (e.g.) scanf("%d", &Avariable) would return the 
  17. >address of Avariable. In C++ I constantly see syntax such as, in 
  18. >declaring a class, Complex pow(const Complex & c, const Complex & power)
  19. >What does the & symbol do here? 
  20.  
  21. Matt,
  22.  
  23. One might as well have a peep in an arbitrary book on C++, nevertheless
  24. following might be of some quick'n dirty little help.
  25.  
  26. The & - "symbol" (as you call it) in C is an OPERATOR, delivering the address
  27. of a variable, as in:
  28.  
  29.     int bla = 0;
  30.     int* p_bla;
  31.  
  32.     p_bla = & bla;
  33.  
  34. This operator usage of '&' is (of course) also useful and allowed in C++.
  35.  
  36. C++ uses the & - "symbol" additionally as part of a TYPE (modifier), called
  37. "reference TYPE".
  38.  
  39.     int  aap = 0;
  40.     int& bla = aap;
  41.  
  42. declares an integer reference - called bla - to aap. One can best think of
  43. such a reference as an ALIAS: where in the code is spoken of 'bla', one might
  44. as well speak of 'aap'.
  45.  
  46.     bla = 4;
  47.  
  48. changes the value of aap to 4.
  49.  
  50. Note that a reference - once created - shall ALWAYS refer to its original
  51. counterpart. Therefore it is always declared using an initializer, which is the
  52. case for the formal parameters in the function you mentioned.
  53.  
  54. On implementation level in C the reference construct can thus best be compared
  55. to
  56.  
  57.     *p_bla = 4
  58.  
  59. which also modifies the value of bla - via a back door.
  60.  
  61. Clear?
  62.     JtH
  63.